/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.FuncRule_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class FuncRule_DAO {
    
     public static String Get_FuncID() throws SQLException{
        String FuncID = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "FD");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            FuncID = Rs.getString(1);
        }
        return FuncID;
    }
    
    public static ArrayList<FuncRule_DTO> GetALL()
    {
        ArrayList<FuncRule_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from DoanTau" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                FuncRule_DTO Func = new FuncRule_DTO();
                Func.setFuncID(rs.getString("FuncID"));
                Func.setFuncName(rs.getString("FuncName"));
                Func.setDienGiai(rs.getString("DienGiai"));
                Func.setActive(rs.getBoolean("Active"));
                Func.setGroupID(rs.getString("GroupID"));
                ds.add(Func);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(FuncRule_DTO FuncRule)
    {
        boolean flag = false;
        try {
            String sql = "Insert FuncRule Values('"+FuncRule.getFuncID()+"','"+FuncRule.getFuncName()+"','"+FuncRule.getDienGiai()+"','"+FuncRule.isActive()+"','"+FuncRule.getGroupID()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(FuncRule_DTO FuncRule)
    {
        boolean flag = false;
        try {
            String sql = "Update FuncRule Set FuncName = '"+ FuncRule.getFuncName() +"', DienGiai = '"+FuncRule.getDienGiai()+"', Active = "+FuncRule.isActive()+", GroupID = '"+ FuncRule.getGroupID()+ "' where FuncID = '"+ FuncRule.getFuncID()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String _FuncID)
    {
        boolean flag = false;
        try {
            String sql = "Delete from FuncRule where FuncID = '"+_FuncID+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
}
